package com.hotent.bpmx.listener;

import com.hotent.bpmx.api.event.NoExecutorEvent;
import com.hotent.bpmx.api.event.NoExecutorModel;
import com.hotent.bpmx.api.model.process.inst.BpmProcessInstance;
import com.hotent.bpmx.api.service.BpmInstService;
import com.hotent.bpmx.plugin.util.DynamicClientUtils;
import com.hotent.bpmx.plugin.util.ProcessConfigUtil;
import javax.annotation.Resource;
import javax.xml.namespace.QName;
import net.sf.json.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Service;

@Service
public class NoExecutorEventListener implements ApplicationListener<NoExecutorEvent>, Ordered {

	@Resource(name = "defaultProcessInstanceService")
	private BpmInstService bpmInstService;

	public int getOrder() {
		return 1;
	}

	public void onApplicationEvent(NoExecutorEvent ev) {
//		NoExecutorModel model = (NoExecutorModel) ev.getSource();
//
//		Client client = null;
//		try {
//			BpmProcessInstance proIns = this.bpmInstService.getProcessInstanceByBpmnInstId(model.getBpmnInstId());
//
//			String adminNotifyUrl = ProcessConfigUtil.getProperty("adminNotifyUrl");
//
//			adminNotifyUrl = adminNotifyUrl.replace("{proInsId}", proIns.getId());
//			adminNotifyUrl = adminNotifyUrl.replace("{bpmnInsId}", proIns.getBpmnInstId());
//			adminNotifyUrl = adminNotifyUrl.replace("{bpmnDefId}", proIns.getBpmnDefId());
//			adminNotifyUrl = adminNotifyUrl.replace("{proDefId}", proIns.getProcDefId());
//			adminNotifyUrl = adminNotifyUrl.replace("{flowStatus}", proIns.getStatus());
//
//			StringBuffer subject = new StringBuffer(model.getSubject());
//			subject.append("的流程，节点：").append("\"").append(model.getNodeName()).append("\"");
//			if ((model.getIdentifyList() != null) && (!model.getIdentifyList().isEmpty())) {
//				subject.append("岗位为空！");
//			} else
//				subject.append("人员为空！");
//
//			String targets = ProcessConfigUtil.getProperty("adminOptUser");
//
//			JSONObject jsonObject = new JSONObject();
//			jsonObject.element("content", "");
//			jsonObject.element("contentTypeFlag", 0);
//			jsonObject.element("source", "BPM");
//			jsonObject.element("targets", targets);
//			jsonObject.element("title", subject.toString());
//
//			jsonObject.element("titleLink", adminNotifyUrl);
//			jsonObject.element("messageType", 1);
//			jsonObject.element("createBy", "");
//
//			client = DynamicClientUtils.getDynamicClient("notifyWsdlAddr");
//			QName qName = new QName("http://BpmNotifyWebService.notity.bpm.esb.ossp.maoye.com", "sendTodoMessage");
//
//			Object[] objects = client.invoke(qName, new Object[] { jsonObject.toString() });
//			JSONObject rtJson = JSONObject.fromObject(objects[0]);
//			if ("1001".equals(rtJson.getString("code")))
//				;
//			System.out.println(objects[0]);
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			client.destroy();
//		}
	}
}